//
//  CircleAnimator.m
//  LvXi_newVersion
//
//  Created by 王敏 on 16/8/12.
//  Copyright © 2016年 SJT. All rights reserved.
//

#import "CircleAnimator.h"


@interface CircleAnimator()
@property (nonatomic, assign) CGPoint startPoint;
@property (nonatomic, assign) CGFloat startRadius;
@property (nonatomic, strong) UIBezierPath *startPath;
@property (nonatomic, strong) UIView *containerView;
@property (nonatomic, strong) CAShapeLayer *maskLayer;

@property(nonatomic,strong)UIView * vvvvv;

@property(nonatomic,strong)UIImageView * imgvvvv;

@property(nonatomic,assign)CGPoint vvvvvStartPoint;

@property(nonatomic,assign)CGPoint vvvvvStopPoint;

@property(nonatomic,assign)CGSize vvvvvOrginSize;




/*********分割线开始*********/

@property(nonatomic,strong)UIImageView * fromeImageView;
@property(nonatomic,strong)UIImageView * toImageView;

/**覆盖层容器视图*/
@property(nonatomic,strong)UIImageView * maskContentView;

/**移动的图片*/
@property(nonatomic,strong)UIImageView * imageView;


@property(nonatomic,assign)BOOL isToAnimation;
/*********分割线结束*********/
@end



@implementation CircleAnimator

+ (instancetype)circleAnimatorWithStartCenter:(CGPoint)point radius:(CGFloat)radius{
    CircleAnimator * animator = [[[self class] alloc] _init];
    animator.startPoint = point;
    return animator;
}

+(instancetype)animatorWithFromImageView:(UIImageView*)fromImageView toImageView:(UIImageView*)toImageView{
    return [[[self class] alloc] _initWithFromImageView:fromImageView toImageView:toImageView];
}

-(id)_init{
    if(self = [super init]){
        
    }
    return self;
}


-(id)_initWithFromImageView:(UIImageView*)fromImageView toImageView:(UIImageView*)toImageView{
    if(self = [super init]){
        self.fromeImageView = fromImageView;
        self.toImageView = toImageView;
        self.isToAnimation = YES;
    }
    return self;
}



- (void)xw_interactiveTransition:(XWInteractiveTransition *)interactiveTransition willEndWithSuccessFlag:(BOOL)flag percent:(CGFloat)percent{
    if (!flag) {
        //防止失败后的闪烁
        _maskLayer.path = _startPath.CGPath;
    }
    _containerView.userInteractionEnabled = YES;
}

-(void)xw_setToAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    UIView * containView = [transitionContext containerView];
    self.isToAnimation = YES;

    self.maskContentView = [[UIImageView alloc] initWithFrame:containView.bounds];
    
    self
    .maskContentView.backgroundColor = HexColor(@"#eeeeee");

    
    [containView addSubview:self.maskContentView];
    
    CGRect adjustFromRect = [self.fromeImageView convertRect:self.toImageView.bounds toView:containView];
    
    CGRect adjustToRect = [self.toImageView convertRect:self.toImageView.bounds toView:containView];
    
    self.imageView = [[UIImageView alloc] initWithFrame:adjustFromRect];
    
    self.imageView.image = self.fromeImageView.image;
    
    [containView addSubview:self.imageView];
    
    self.imageView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.imageView.layer.shadowOpacity = 0.5;
    self.imageView.layer.shadowOffset = CGSizeMake(0, 0);
    
    //阴影动画1
    CABasicAnimation * shadowAnimator1 = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];
    shadowAnimator1.fromValue=[NSValue valueWithCGSize:CGSizeMake(0, 0)];
    shadowAnimator1.toValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
    shadowAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    shadowAnimator1.fillMode = kCAFillModeForwards;
    shadowAnimator1.removedOnCompletion = NO;
    shadowAnimator1.duration = 0.6;
    shadowAnimator1.beginTime = 0;
    
    
    //缩放动画1
    CABasicAnimation * scaleAnimator1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimator1.toValue = @(1.2);
    scaleAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimator1.fillMode = kCAFillModeForwards;
    scaleAnimator1.removedOnCompletion = NO;
    scaleAnimator1.duration = 0.6;
    scaleAnimator1.beginTime = 0;
    
    //移动动画1
    CABasicAnimation * moveAnimator1 = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimator1.toValue = [NSValue valueWithCGPoint:CGPointMake(adjustToRect.origin.x+adjustToRect.size.width*0.5, adjustToRect.origin.y+adjustToRect.size.height*0.5)];
    moveAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    moveAnimator1.fillMode = kCAFillModeForwards;
    moveAnimator1.removedOnCompletion = NO;
    moveAnimator1.duration = 0.8;
    moveAnimator1.beginTime = 0;
    

    
    
    //缩放动画2
    CABasicAnimation * scaleAnimator2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimator2.toValue = @(1);
    scaleAnimator2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimator2.fillMode = kCAFillModeForwards;
    scaleAnimator2.removedOnCompletion = NO;
    scaleAnimator2.duration = 0.2;
    scaleAnimator2.beginTime = 0.6;
    
    
    //阴影动画2
    CABasicAnimation * shadowAnimator2 = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];
    shadowAnimator2.fromValue=[NSValue valueWithCGSize:CGSizeMake(10, 10)];
    shadowAnimator2.toValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)];
    shadowAnimator2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    shadowAnimator2.fillMode = kCAFillModeForwards;
    shadowAnimator2.removedOnCompletion = NO;
    shadowAnimator2.duration = 0.2;
    shadowAnimator2.beginTime = 0.6;
    
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.animations = @[shadowAnimator1,moveAnimator1,scaleAnimator1,scaleAnimator2,shadowAnimator2];
    group.duration = 0.8;
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    group.delegate = self;
    [group setValue:transitionContext forKey:@"transitionContext"];
    [self.imageView.layer addAnimation:group forKey:nil];
}




-(void)xw_setBackAnimation:(id<UIViewControllerContextTransitioning>)transitionContext{
    //从视图
    UIView * fromView = [transitionContext viewForKey:UITransitionContextFromViewKey];
    //容器视图
    UIView * containView = [transitionContext containerView];
    self.isToAnimation = NO;
    //动画覆盖层
    self.maskContentView = [[UIImageView alloc] initWithFrame:containView.bounds];
    self
    .maskContentView.backgroundColor = HexColor(@"#eeeeee");
    
    self.maskContentView.bounds = containView.bounds;
    [containView addSubview:self.maskContentView];
    
    
    CGRect adjustFromRect = [self.fromeImageView convertRect:self.toImageView.bounds toView:containView];
    
    CGRect adjustToRect = [self.toImageView convertRect:self.toImageView.bounds toView:containView];
    
    self.imageView = [[UIImageView alloc] initWithFrame:adjustToRect];
    self.imageView.image = self.fromeImageView.image;
    
    [containView addSubview:self.imageView];
    
    [containView bringSubviewToFront:self.imageView];
    [containView sendSubviewToBack:self.maskContentView];
    
    CGRect adjustFrame = [self.imageView convertRect:self.imageView.bounds toView:self.maskContentView];
    
    
    
    self.imageView.layer.shadowColor = [UIColor blackColor].CGColor;
    self.imageView.layer.shadowOpacity = 0.5;
    self.imageView.layer.shadowOffset = CGSizeMake(0, 0);
    //阴影动画1
    CABasicAnimation * shadowAnimator1 = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];
    shadowAnimator1.fromValue=[NSValue valueWithCGSize:CGSizeMake(0, 0)];
    shadowAnimator1.toValue = [NSValue valueWithCGSize:CGSizeMake(10, 10)];
    shadowAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    shadowAnimator1.fillMode = kCAFillModeForwards;
    shadowAnimator1.removedOnCompletion = NO;
    shadowAnimator1.beginTime = 0;
    shadowAnimator1.duration = 0.6;
    
    
    //移动动画1
    CABasicAnimation * moveAnimator1 = [CABasicAnimation animationWithKeyPath:@"position"];
    moveAnimator1.toValue = [NSValue valueWithCGPoint:CGPointMake(adjustFromRect.origin.x+adjustFromRect.size.width*0.5, adjustFromRect.origin.y+adjustFromRect.size.height*0.5)];
    moveAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    moveAnimator1.fillMode = kCAFillModeForwards;
    moveAnimator1.removedOnCompletion = NO;
    moveAnimator1.beginTime = 0;
    moveAnimator1.duration = 0.8;
    
    
    //缩放动画1
    CABasicAnimation * scaleAnimator1 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimator1.toValue = @(1.2);
    scaleAnimator1.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimator1.fillMode = kCAFillModeForwards;
    scaleAnimator1.removedOnCompletion = NO;
    scaleAnimator1.beginTime = 0;
    scaleAnimator1.duration = 0.6;
    
    //缩放动画2
    CABasicAnimation * scaleAnimator2 = [CABasicAnimation animationWithKeyPath:@"transform.scale"];
    scaleAnimator2.toValue = @(1);
    scaleAnimator2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    scaleAnimator2.fillMode = kCAFillModeForwards;
    scaleAnimator2.removedOnCompletion = NO;
    scaleAnimator2.beginTime = 0.6;
    scaleAnimator2.duration = 0.2;
    
    //阴影动画2
    CABasicAnimation * shadowAnimator2 = [CABasicAnimation animationWithKeyPath:@"shadowOffset"];
    shadowAnimator2.fromValue=[NSValue valueWithCGSize:CGSizeMake(10, 10)];
    shadowAnimator2.toValue = [NSValue valueWithCGSize:CGSizeMake(0, 0)];
    shadowAnimator2.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    shadowAnimator2.fillMode = kCAFillModeForwards;
    shadowAnimator2.removedOnCompletion = NO;
    shadowAnimator2.beginTime = 0.6;
    shadowAnimator2.duration = 0.2;
    
    
    CAAnimationGroup * group = [CAAnimationGroup animation];
    group.animations = @[shadowAnimator1,moveAnimator1,scaleAnimator1,scaleAnimator2,shadowAnimator2];
    group.duration = 0.8;
    group.fillMode = kCAFillModeForwards;
    group.removedOnCompletion = NO;
    group.delegate = self;
    [group setValue:transitionContext forKey:@"transitionContext"];
    
    [self maskFromView:fromView toRect:adjustFrame duration:0.4 complete:^{
        [self.imageView.layer addAnimation:group forKey:nil];
    }];
}

- (void)animationDidStop:(CAAnimation *)anim finished:(BOOL)flag{
    id<UIViewControllerContextTransitioning> transitionContext = [anim valueForKey:@"transitionContext"];
    
    UIView * containView = [transitionContext containerView];
    
    UIView * toView = [transitionContext viewForKey:UITransitionContextToViewKey];

    [containView addSubview:toView];
   
        if(self.isToAnimation){
        [containView addSubview:toView];
        CGRect adjustFrame = [self.toImageView convertRect:self.imageView.bounds toView:self.maskContentView];
        [self maskToView:toView toRect:adjustFrame duration:0.2 complete:^{
            [self.imageView removeFromSuperview];
            [self.maskContentView removeFromSuperview];
            [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
        }];
    }else{
        [self.imageView removeFromSuperview];
        [self.maskContentView removeFromSuperview];
         [transitionContext completeTransition:![transitionContext transitionWasCancelled]];
    }
    
    
}
-(void)maskToView:(UIView*)toView toRect:(CGRect)toRect duration:(NSTimeInterval)duration complete:(void(^)())complete{
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        complete();
    }];
    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    CGPoint fromCenter = CGPointMake(toRect.origin.x+toRect.size.width*0.5, toRect.origin.y+toRect.size.height*0.5);
    CGFloat fromRadius = MIN(toRect.size.width/2,toRect.size.height/2);
    
    UIBezierPath * fromPath = [UIBezierPath bezierPathWithArcCenter:fromCenter radius:fromRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    CGFloat viewWidth = toView.width;
    CGFloat viewHeight = toView.height;
    
    CGFloat r1 = sqrt(fromCenter.x * fromCenter.x + fromCenter.y * fromCenter.y);
    CGFloat r2 = sqrt((fromCenter.x - viewWidth)  * (fromCenter.x - viewWidth) + fromCenter.y * fromCenter.y);
    CGFloat r32 = (fromCenter.x - viewWidth)  * (fromCenter.x - viewWidth) + (fromCenter.y - viewHeight) * (fromCenter.y - viewHeight);
    CGFloat r3 = sqrt(r32);
    CGFloat r4 = sqrt(fromCenter.x * fromCenter.x + (fromCenter.y - viewHeight) * (fromCenter.y - viewHeight));
    CGFloat toRadius = MAX(MAX(MAX(r1,r2),r3),r4);
    
    UIBezierPath * toPath = [UIBezierPath bezierPathWithArcCenter:fromCenter radius:toRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    maskLayer.path = toPath.CGPath;
    CABasicAnimation * basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = duration;
    basicAnimation.fromValue = (__bridge id)(fromPath.CGPath);
    basicAnimation.toValue = (__bridge id)(toPath.CGPath);
    basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskLayer addAnimation:basicAnimation forKey:nil];
    toView.layer.mask = maskLayer;
    [CATransaction commit];
}

-(void)maskFromView:(UIView*)fromView toRect:(CGRect)toRect duration:(NSTimeInterval)duration complete:(void(^)())complete{
    [CATransaction begin];
    [CATransaction setCompletionBlock:^{
        complete();
    }];
    CAShapeLayer * maskLayer = [CAShapeLayer layer];
    CGPoint toCenter = CGPointMake(toRect.origin.x+toRect.size.width*0.5, toRect.origin.y+toRect.size.height*0.5);
    
    CGFloat toRadius = 0.001;
    UIBezierPath * toPath = [UIBezierPath bezierPathWithArcCenter:toCenter radius:toRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    CGFloat viewWidth = fromView.width;
    CGFloat viewHeight = fromView.height;
    
    CGFloat r1 = sqrt(toCenter.x * toCenter.x + toCenter.y * toCenter.y);
    CGFloat r2 = sqrt((toCenter.x - viewWidth)  * (toCenter.x - viewWidth) + toCenter.y * toCenter.y);
    CGFloat r32 = (toCenter.x - viewWidth)  * (toCenter.x - viewWidth) + (toCenter.y - viewHeight) * (toCenter.y - viewHeight);
    CGFloat r3 = sqrt(r32);
    CGFloat r4 = sqrt(toCenter.x * toCenter.x + (toCenter.y - viewHeight) * (toCenter.y - viewHeight));
    CGFloat fromRadius = MAX(MAX(MAX(r1,r2),r3),r4);
    UIBezierPath * fromPath = [UIBezierPath bezierPathWithArcCenter:toCenter radius:fromRadius startAngle:0 endAngle:M_PI*2 clockwise:YES];
    
    maskLayer.path = toPath.CGPath;
    
    
    CABasicAnimation * basicAnimation = [CABasicAnimation animationWithKeyPath:@"path"];
    basicAnimation.duration = duration;
    basicAnimation.fromValue = (__bridge id)(fromPath.CGPath);
    basicAnimation.toValue = (__bridge id)(toPath.CGPath);
    basicAnimation.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    [maskLayer addAnimation:basicAnimation forKey:nil];
    fromView.layer.mask = maskLayer;
    [CATransaction commit];
}
@end
